import axios from '../../utils/request'
import distUtils from "../../utils/distUtils";
const state = {
  moduleData:{}
}

//方法参考地址
//https://www.jb51.net/article/261401.htm
const mutations = {
//保存列表信息
  setModuleData: (state, data) => {
    state.moduleData = data
  },

}
const actions = {

  /**
   * 通过模板id查询单词考试内容
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  selectModuleDataById: function ({commit}, params) {

    return new Promise((resolve, reject) => {
      axios.get('/wordModule/findDetailByModuleId',{params:params}).then((res) => {
        const moduleData = res.data
        commit("setModuleData", moduleData)
        // console.log("============wordModule.js==== " + JSON.stringify(state.moduleData))
        resolve(res)
      }).catch((error) => {
        reject(error)
      })

    }).catch(error=>{
      console.log(error)
    })


  },


  /**
   * 查询模板列表信息
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  selectModuleDistList: function ({commit}, params) {

    return new Promise((resolve, reject) => {
      axios.get('/wordModule/list',{params:params}).then((res) => {
        // console.log("============res111 " + JSON.stringify(res))
        const moduleList = distUtils.getDataList(res.data,"全部模板","id","moduleName")
        resolve(moduleList)
      }).catch((error) => {
        reject(error)
      })

    }).catch(error=>{
      console.log(error)
    })


  },
  /**
   * 获取分页数据
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  getListPageData: function ({commit}, params) {
    // console.log('params ' +JSON.stringify(params))
    return new Promise((resolve, reject) => {
      // const params = {page:1,pageSize:1}; // 参数
      axios.get('/wordModule/listPage', {params:params}).then((res) => {
          const dataValue = res.data;
          resolve(dataValue)
        }).catch((error) => {
          reject(error)
        })

    }).catch(error=>{
      console.log(error)
    })

  },

  /**
   * 更新模板数据
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  updateWordModule: function ({commit}, from) {
    return new Promise((resolve, reject) => {
      axios.post('/wordModule/saveOrUpdate', from).then((res) => {
        resolve(res)
      }).catch((error) => {
        reject(error)
      })

    }).catch(error=>{
      console.log(error)
    })

  },

  /**
   * 删除模板数据
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  deleteWordModule: function ({commit}, ids) {
    return new Promise((resolve, reject) => {
      axios.delete('/wordModule/deleteByIds?ids='+ids).then((res) => {
        resolve(res)
      }).catch((error) => {
        reject(error)
      })
    }).catch(error=>{
      console.log(error)
    })

  },


  /**
   * 查询模板明细-通过id
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  selectWordDataById: function ({commit}, wordModuleId) {
    // console.log('params ' +JSON.stringify(wordModuleId))
    return new Promise((resolve, reject) => {
      // const params = {page:1,pageSize:1}; // 参数
      axios.get('/wordModuleDetail/list?wordModuleId='+wordModuleId).then((res) => {
        // console.log("wordDataList " +JSON.stringify(res));
        resolve(res)
      }).catch((error) => {
        reject(error)
      })

    }).catch(error=>{
      console.log(error)
    })

  },

  /**
   * 保存模板明细
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  saveWordData: function ({commit}, data) {
    // console.log('params ' +JSON.stringify(wordModuleId))
    return new Promise((resolve, reject) => {
      // const params = {page:1,pageSize:1}; // 参数
      axios.post('/wordModuleDetail/saveOrUpdate',data).then((res) => {
        resolve(res)
      }).catch((error) => {
        reject(error)
      })

    }).catch(error=>{
      console.log(error)
    })

  },

  /**
   * 删除模板明细
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  deleteWordData: function ({commit}, id) {
    return new Promise((resolve, reject) => {
      axios.delete('/wordModuleDetail/deleteByIds?ids='+id).then((res) => {
        resolve(res)
      }).catch((error) => {
        reject(error)
      })
    }).catch(error=>{
      console.log(error)
    })

  },

}


export default {
  namespaced: true,
  state,
  mutations,
  actions
}
